import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.feature_extraction.text import TfidfTransformer, CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn import svm


def load_file(path):
    data = pd.read_csv(path, encoding="utf-8")
    array = data["remark"][10500:12000]
    label = list(data["id"][10500:12000])
    label = np.array(label)
    # 1 positive 好评, 0  negative 差评
    return array, label


# 文本特征提取，数据降维
def data_processing(array):
    vectorized = CountVectorizer()
    transformer = TfidfTransformer()
    # 词频统计
    count = vectorized.fit_transform(array.values.astype('U'))
    tfidf_matrix = transformer.fit_transform(count)
    matrix = tfidf_matrix.toarray()
    # Pca模型
    pca = PCA(n_components=5)
    pca.fit(matrix)
    # size(数据，特征维度)
    new_matrix = pca.fit_transform(matrix)
    return new_matrix


# svm模型
def svm_function(x_train, x_test, y_train, y_test):
    clf = svm.SVC()
    clf.fit(X=x_train, y=y_train, sample_weight=None)
    # 预测标签
    result = clf.predict(x_test)
    print(result)
    # 预测准确值
    score = clf.score(x_test, y_test)
    print(score)
    return score


def main():
    path = "D:/gitee/nlpLing/sentiment-analysis/svmNlp.csv"
    data, label = load_file(path)
    data_processed = data_processing(data)
    # 数据随机分割
    x_train, x_test, y_train, y_test = train_test_split(data_processed, label, test_size=0.3)
    score = svm_function(x_train, x_test, y_train, y_test)
    return score


if __name__ == '__main__':
    main()
